import request from '@/utils/request'
// import chunkApi from '@/utils/chunkApi'

// export function AI(data) {
//   return chunkApi('/detect', data)
// }

export function AI(data) {
  return request({
    url: '/detect',
    method: 'POST',
    data: data
  })
}

export async function chunkApi(data, chunkSize = 10) {
  let respones = [];

  for (let i = 0; i < Math.ceil(data.length / chunkSize); i++) {
    const chunk = data.slice(i * chunkSize, (i + 1) * chunkSize);
    const res = await AI(chunk);
    respones = respones.concat(res);
  }

  return respones
}

export function AIresult(respones, templates) {
  const t = templates.data;

  const result = respones.map(v => {
    for (let key in v) {
      v[key] = v[key].map(u => {
        const point = {
          x: (u[0] + u[1]) / 2,
          y: (u[2] + u[3]) / 2,
        }
        const index = t.findIndex(w => {
          return (point.x >= w.left && point.y >= w.top && point.x < (w.left + w.width) && point.y < (w.top + w.height))
        })
        return index
      }).sort((f1, f2) => f1 - f2)
    }
    return v;
  })

  console.log(result);
  return result
}